#include "stdafx.h"
#include "wf_drawpoint.h"
#include "pf_snapper.h"
#include "pf_point.h"



WF_DrawPoint::WF_DrawPoint(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Points",
                           container, graphicView) {}


QAction* WF_DrawPoint::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// (tr("Points")
	QAction* action = new QAction(tr("&Points"),  NULL);
	action->setIcon(QIcon(":/extui/points.png"));
    //action->zetStatusTip(tr("Draw Points"));
	
    return action;
}


void WF_DrawPoint::trigger() {
    if (pt.valid) {
        PF_Point* point = new PF_Point(container, PF_PointAttribute(pt));
        container->addEntity(point);

        if (document) {
            document->startUndoCycle();
            document->addUndoable(point);
            document->endUndoCycle();
        }

		graphicView->redraw(LX_Define::RedrawDrawing);
        graphicView->moveRelativeZero(pt);
    }
}



void WF_DrawPoint::mouseMoveEvent(PF_MouseEvent* e) {
    snapPoint(e);
}



void WF_DrawPoint::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    }
}



void WF_DrawPoint::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    pt = mouse;
    trigger();
}



void WF_DrawPoint::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }
}



PF_StringList WF_DrawPoint::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}


void WF_DrawPoint::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case 0:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify location"), tr("Cancel"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawPoint::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawPoint::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarPoints);
        }
    }
}

// EOF
